/**
 *  Copyright 2011 Pavlov Dm.
 *  Licensed under the MIT License
 */

package org.motiv;

import org.apache.log4j.BasicConfigurator;
import java.util.Random;

import org.motiv.core.CacheManager;
import org.motiv.core.Cache;
import org.motiv.core.Element;
import org.motiv.tests.policy.ElementFactory;

public class L2Cache {
    public static void main(String[] args) {
		BasicConfigurator.configure();
		CacheManager manager = CacheManager.getInstance(); 
		
		
		Cache inMemoryCache = manager.getCache("inMemoryOnly");
		if(inMemoryCache!=null){
		
			for(int iObjIndex=1;iObjIndex<13;iObjIndex++){
				inMemoryCache.put(ElementFactory.create(new Long(iObjIndex),"name"+Integer.toString(iObjIndex)));
			}
			
			int iAttempt = 1;
			
			Random rand = new Random();
			
			while( iAttempt < 6 ){
			
				Long key = new Long(rand.nextInt(11))+1L;
			
				Element element = inMemoryCache.get(key);
				
				if(element != null){
					System.out.println("Get element from cache: \n"+element.toString());
				} else {
					System.out.println("Object with key "+key.toString()+" not in cache");
				}
				
				iAttempt ++;
			}
			
			inMemoryCache.dispose();
		} 
		
		Cache overflowCache = manager.getCache("overflowToDisk");
		
		if(overflowCache!=null){
			
			for(int iObjIndex=1;iObjIndex<9;iObjIndex++){
				overflowCache.put(ElementFactory.create(new Long(iObjIndex),"name"+Integer.toString(iObjIndex)));
			}
			
			int iAttempt = 1;
			
			Random rand = new Random();
			
			while( iAttempt < 10 ){
			
				Long key = new Long(rand.nextInt(7))+1L;

				Element element = overflowCache.get(key);
				
				if(element != null){
					System.out.println("Get element from cache: \n"+element.toString());
				} else {
					System.out.println("Object with key "+key.toString()+" not in cache");
				}
				
				iAttempt ++;
			}
			
			overflowCache.dispose();
		}
		
    }
}